package com.meixiang.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.account.manager.AddressActivity;
import com.meixiang.entity.account.AddressListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 地址管理
 */
public class AddressManageAdapter extends RecyclerView.Adapter<AddressManageAdapter.ViewHolder> {

    private List<AddressListEntity> mAddressList = new ArrayList<>();
    private Activity mContext;

    public AddressManageAdapter(Activity context) {
        this.mContext = context;
    }

    public void addData(List<AddressListEntity> list) {
        this.mAddressList.addAll(list);
        this.notifyDataSetChanged();
    }

    public void removeData(int position) {
        this.mAddressList.remove(position);
        this.notifyDataSetChanged();
    }

    public List<AddressListEntity> getData() {
        return mAddressList;
    }

    public void clearData() {
        this.mAddressList.clear();
        this.notifyDataSetChanged();
    }

    /**
     * 刷新数据
     */
    public void refreshData(List<AddressListEntity> mList) {
        this.mAddressList = mList;
        this.notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_manage_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final AddressListEntity addressList = mAddressList.get(position);
        if (addressList == null) return;
        holder.mTvAddressUser.setText(addressList.getTrueName());
        holder.mTvAddressPhone.setText(addressList.getMobPhone());
        holder.mTvAddress.setText(addressList.getAreaInfo() + "" + addressList.getAddress());

        //isDefault为1默认选中，否则不选中
        if (addressList.getIsDefault().equals("1")) {
            holder.mCkSetDefault.setChecked(true);
        } else {
            holder.mCkSetDefault.setChecked(false);
        }


        //设置默认
        holder.mCkSetDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mListener != null) {
                    mListener.onSetDefaultItemClick(v, position);
                }
            }
        });
        //编辑
        holder.mTvAddressRedact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AddressActivity.class);
                intent.putExtra("status", "1");
                intent.putExtra("addressId", addressList.getAddressId());
                mContext.startActivity(intent);
            }
        });


        //删除
        holder.mTvAddressDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onDeleteItemClick(v, position);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return mAddressList == null ? 0 : mAddressList.size();
    }

    /**
     * 单击事件监听器
     */
    private onAddressItemClickListener mListener = null;

    public void setOnAddressItemClickListener(onAddressItemClickListener listener) {
        mListener = listener;
    }

    public interface onAddressItemClickListener {
        /**
         * 设置默认点击事件
         *
         * @param v
         * @param position
         */
        void onSetDefaultItemClick(View v, int position);

        /**
         * 删除点击事件
         *
         * @param v
         * @param position
         */
        void onDeleteItemClick(View v, int position);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_address_user)
        TextView mTvAddressUser;
        @Bind(R.id.tv_address_phone)
        TextView mTvAddressPhone;
        @Bind(R.id.tv_address)
        TextView mTvAddress;
        @Bind(R.id.ck_set_default)
        CheckBox mCkSetDefault;
        @Bind(R.id.tv_address_redact)
        TextView mTvAddressRedact;
        @Bind(R.id.tv_address_delete)
        TextView mTvAddressDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
